import gameobject
from gameobject import *

import pygame

import random

from pygame import *

import joshfunctions
from joshfunctions import *

class Enemy(GameObject):
    def __init__(self, position, spritesheet, totaltime):
        GameObject.__init__(self)
        self.type = "enemy"
        self.destroyed = False
        self.position = (position[0] - 512, position[1]-288)
        self.rect = Rect(0.0,0.0,8.0,8.0)
        self.rect.center = self.position
        self.frames = [[0,0,0,0],[0,0,0,0]]
        self.scaledframes = [[0,0,0,0],[0,0,0,0]]
        self.warpframes = [0,0,0,0]
        self.warpx = [8,11,10,9]
        self.warpframe = 0
        self.facingright = True
        self.speed = .3
        self.animationcountdown = self.speed
        self.blinkingcountdown = self.speed / 3.0
        self.frame = 0
        self.velocity = 24.0+60*random.random()
        self.keyMap={"left":False, "right":False, "up":False,"down":False, "shoot": False, "attack": False}
        self.blinking = False
        self.blinkingonoff = False
        self.currenttarget = (0,0)
        self.finalvect = (0,0)
        self.warping = True
        self.warpcountdown = 1.5+random.random()
        self.repulseradius = 24
        self.health = 1.0+2.0*random.random()+3.0*(totaltime/120)
        self.item = True
        self.damage = 3.0
        spritepos = [random.randrange(1,12), random.randrange(1,12)]
        while (spritepos == (5,3) or spritepos == (5,1)):
            spritepos = [random.randrange(0,12), random.randrange(0,12)]
            
        self.colorframe = 3
        for i in range(4):
            self.warpframes[i]= Surface((8,8))
            self.warpframes[i].set_colorkey((0,255,255))
            self.warpframes[i].blit(spritesheet, (0,0), ((self.warpx[i]*8),(13*8),8,8))
            self.warpframes[i] = transform.scale(self.warpframes[i], (32, 32))
            for x in range(self.warpframes[i].get_width()):
                for y in range(self.warpframes[i].get_height()):
                    currentcolor = self.warpframes[i].get_at((x,y))

                    if currentcolor == (0,0,0, 255):
                        self.warpframes[i].set_at((x,y),(139,149,109))

        for i in range(2):
            for j in range(4):
                self.frames[i][j] = Surface((8,8))
                self.frames[i][j].set_colorkey((0,255,255))
                self.frames[i][j].blit(spritesheet, (0,0), ((spritepos[0]*8)+(96*i),(spritepos[1]*8),8,8))
            for x in range(self.frames[i][0].get_width()):
                for y in range(self.frames[i][0].get_height()):
                    currentcolor = self.frames[i][0].get_at((x,y))

                    if currentcolor == (0,0,0, 255):
                        self.frames[i][0].set_at((x,y),(196,207,161))
                        self.frames[i][1].set_at((x,y),(139,149,109))
                        self.frames[i][2].set_at((x,y),(74,81,57))
                        self.frames[i][3].set_at((x,y),(31,31,31,255))




        for i in range(2):
            for j in range(4):
                self.scaledframes[i][j] = transform.scale(self.frames[i][j], (self.frames[i][j].get_width()*4,self.frames[i][j].get_height()*4))

        #print "Enemy made with ", spritepos, " and location ", self.position
    def update(self, etime, camera, players, enemies):
        if self.warping:
            self.animationcountdown -= etime
            if self.animationcountdown <= 0.0:
                self.warpframe = self.warpframe + 1 if self.warpframe < len(self.warpframes)-1 else 0
                self.animationcountdown = self.speed*.5 * (1.0 + random.random() / 4)

            self.warpcountdown -= etime
            if self.warpcountdown <= 0.0:
                self.warping = False
                self.blinkingonoroff = False
                self.blinking = False
                
                
            if self.blinkingonoff:
                self.blinkingcountdown -= etime
                if self.blinkingcountdown <= 0.0:
                    self.blinking = True if self.blinking == False else False
                    self.blinkingcountdown = .1*(2.0+random.random())
            else:
                self.blinking = False
                self.blinkingcountdown = 0.0

        else:
            self.animationcountdown -= etime
            if self.animationcountdown <= 0.0:
                self.frame = 1 if self.frame == 0 else 0
                self.animationcountdown = self.speed*(1.0+random.random()/4)
            currentx = self.position[0]
            currenty = self.position[1]

            # check for closer player
            playergoalposition = (0,0)
            if len(players) >= 2:

                playerdistances = [dist(self.position, players[0].position), dist(self.position, players[1].position)]
                if playerdistances[0] <= playerdistances[1]:
                    #print "targetting player 1"
                    playergoalposition = players[0].position
                else:
                    #print "targetting player 2"
                    playergoalposition = players[1].position


            else:
                playergoalposition = players[0].position

            playergoalposition = (playergoalposition[0]+16, playergoalposition[1]+16)
            if playergoalposition[0]-self.position[0]>=0:
                self.facingright = True
            else:
                self.facingright = False

            self.repulseradius = 45
            for player in players:
                position = (player.drawrect.topleft[0]+16,player.drawrect.topleft[1]+16 )
                #print self.drawrect.center, position, dist(self.drawrect.center, position)
                if dist(self.drawrect.center, position) <= 32:
                    self.destroyed = True
                    self.item = False
                    player.health -= self.damage*(1.0/player.combo)


            flockingvect = (0,0)
            velocityvect = (0,0)

            for i in range(len(players)):
                #print self.position, players[i].position
                pass
            if len(enemies) < 50:
                velocitynum = 1
                for enemy in enemies:
                    if enemy!= self and enemy.warping == False:
                        if dist(self.position, enemy.position) <= self.repulseradius:
                            flockingvect = (flockingvect[0]+(enemy.position[0]-self.position[0])*2*enemy.repulseradius,flockingvect[1]+(enemy.position[1]-self.position[1])*2*enemy.repulseradius)
                            velocityvect = (velocityvect[0]+enemy.finalvect[0],velocityvect[1]+enemy.finalvect[1])
                            velocitynum += 1
                velocityvect = ((velocityvect[0]/velocitynum),(velocityvect[1]/velocitynum))
                
           
                    
            self.finalvect = (playergoalposition[0]-flockingvect[0]+velocityvect[0],playergoalposition[1]-flockingvect[1]+velocityvect[1])
            angletoplayer = angleBetweenPts(self.finalvect, self.position)
            currentx -= dx(self.velocity*etime, angletoplayer)
            currenty -= dy(self.velocity*etime, angletoplayer)
            self.position = (currentx, currenty)
        self.rect.center = self.position
        self.drawrect = self.rect.copy()
        self.drawrect = self.drawrect.inflate(32.0, 32.0)
        self.drawrect.left -= camera[0]
        self.drawrect.top -= camera[1]
        





    def drawbottom(self, surf, camera, night):
        if self.blinking == True:
            return
        if self.warping:
            return
        else:
            if night:
                shadowcolor = 2
            else:
                shadowcolor = 1
            surf.blit(self.scaledframes[self.frame][shadowcolor] if self.facingright else transform.flip(self.scaledframes[self.frame][shadowcolor], True, False), (self.drawrect.left+(.5*4.0), self.drawrect.top+(.5*4.0)))

    def drawtop(self, surf, camera,night):
        if self.blinking == True:
            return
        if self.warping:
            surf.blit(self.warpframes[self.warpframe], self.drawrect.topleft)
        else:
            if night:
                spritecolor = 1
            else:
                spritecolor = 2
            surf.blit(self.scaledframes[self.frame][spritecolor] if self.facingright else transform.flip(self.scaledframes[self.frame][spritecolor], True, False), self.drawrect.topleft)
        #draw.circle(surf, (255,0,0), (self.drawrect.center), 24, 1)
        #print (self.position[0]-camera[0],self.position[1]-camera[1])
        #draw.rect(surf, (0,255,0,128), self.drawrect)